//===================================================
//作    者：边涯  http://www.u3dol.com
//创建时间：2020-08-05 19:19:53
//备    注：
//===================================================
//using YouYouServer.Core;
using System;
using System.Collections.Generic;

/// <summary>
/// 客户端发送战斗胜利消息
/// </summary>
public struct GameLevel_VictoryProto : IProto
{
    public ushort ProtoCode => 12003;
    public string ProtoEnName => "GameLevel_Victory";
    //public ProtoCategory Category => ProtoCategory.Client2GameServer;

    public int GameLevelId; //游戏关卡Id
    public byte Grade; //难度等级
    public byte Star; //获得星级
    public int Exp; //获得经验
    public int Gold; //获得金币
    public int KillTotalMonsterCount; //杀怪数量
    public List<MonsterItem> KillMonsterList; //杀怪列表
    public int GoodsTotalCount; //获得物品数量
    public List<GoodsItem> GetGoodsList; //获得物品

    [Serializable]
    /// <summary>
    /// 杀怪列表
    /// </summary>
    public struct MonsterItem
    {
        public int MonsterId; //怪Id
        public int MonsterCount; //怪数量
    }

    [Serializable]
    /// <summary>
    /// 获得物品
    /// </summary>
    public struct GoodsItem
    {
        public byte GoodsType; //物品类型
        public int GoodsId; //物品Id
        public int GoodsCount; //物品数量
    }

    public byte[] ToArray(MMO_MemoryStream ms, bool isChild = false)
    {
        ms.SetLength(0);
        if (!isChild)
        {
            ms.WriteUShort(ProtoCode);
            //ms.WriteByte((byte)Category);
        }

        ms.WriteInt(GameLevelId);
        ms.WriteByte(Grade);
        ms.WriteByte(Star);
        ms.WriteInt(Exp);
        ms.WriteInt(Gold);
        ms.WriteInt(KillTotalMonsterCount);
        for (int i = 0; i < KillTotalMonsterCount; i++)
        {
            var item = KillMonsterList[i];
            ms.WriteInt(item.MonsterId);
            ms.WriteInt(item.MonsterCount);
        }
        ms.WriteInt(GoodsTotalCount);
        for (int i = 0; i < GoodsTotalCount; i++)
        {
            var item = GetGoodsList[i];
            ms.WriteByte(item.GoodsType);
            ms.WriteInt(item.GoodsId);
            ms.WriteInt(item.GoodsCount);
        }

        return ms.ToArray();
    }

    public static GameLevel_VictoryProto GetProto(MMO_MemoryStream ms, byte[] buffer)
    {
        GameLevel_VictoryProto proto = new GameLevel_VictoryProto();
        ms.SetLength(0);
        ms.Write(buffer, 0, buffer.Length);
        ms.Position = 0;

        proto.GameLevelId = ms.ReadInt();
        proto.Grade = (byte)ms.ReadByte();
        proto.Star = (byte)ms.ReadByte();
        proto.Exp = ms.ReadInt();
        proto.Gold = ms.ReadInt();
        proto.KillTotalMonsterCount = ms.ReadInt();
        proto.KillMonsterList = new List<MonsterItem>();
        for (int i = 0; i < proto.KillTotalMonsterCount; i++)
        {
            MonsterItem _KillMonster = new MonsterItem();
            _KillMonster.MonsterId = ms.ReadInt();
            _KillMonster.MonsterCount = ms.ReadInt();
            proto.KillMonsterList.Add(_KillMonster);
        }
        proto.GoodsTotalCount = ms.ReadInt();
        proto.GetGoodsList = new List<GoodsItem>();
        for (int i = 0; i < proto.GoodsTotalCount; i++)
        {
            GoodsItem _GetGoods = new GoodsItem();
            _GetGoods.GoodsType = (byte)ms.ReadByte();
            _GetGoods.GoodsId = ms.ReadInt();
            _GetGoods.GoodsCount = ms.ReadInt();
            proto.GetGoodsList.Add(_GetGoods);
        }

        return proto;
    }
}